﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.Foundation.Collections;
using Xiaowei.Diagnostics;
using Xiaowei.Services;

namespace Xiaowei.Players
{
    public class MusicPlayer : IPlayer
    {
        private Queue<KeyValuePair<PlayerCommandType, object>> commandList = new Queue<KeyValuePair<PlayerCommandType, object>>();
        public MusicPlayer()
        {
            PlayStateCombine = new PlayStateCombine()
            {
                PlayerState = PlayerState.Stop,
                ID = "",
                Title=""
            };
            ServiceFactory.QQMusicService.PlayStateChanged += PlayStateChanged;
            try
            {
                GetCurrentState();
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }
        private void PlayStateChanged(int State, string ID, bool IsInstalled, bool IsStarted)
        {
            PlayerState playerState = PlayerState.Stop;
            switch (State)
            {
                case 1:
                    playerState = PlayerState.Buffering;
                    break;
                case 2:
                    playerState = PlayerState.Play;
                    break;
                case 3:
                    playerState = PlayerState.Pause;
                    break;
                default:
                    break;
            }
            if (PlayStateCombine.ID != ID || PlayStateCombine.PlayerState != playerState)
            {
                PlayStateCombine.ID = ID;
                PlayStateCombine.PlayerState = playerState;
                PlayerStateChanged?.Invoke(this, PlayStateCombine);
            }
            if (this.IsInstalled != IsInstalled)
            {
                this.IsInstalled = IsInstalled;
            }
            if (this.IsLaunched != IsStarted)
            {
                this.IsLaunched = IsStarted;
            }
        }
        private void GetCurrentState()
        {
            PlayStateChanged(
                ServiceFactory.QQMusicService.PlayerStatus,
                ServiceFactory.QQMusicService.PlayerSongID,
                ServiceFactory.QQMusicService.IsInstalled,
                ServiceFactory.QQMusicService.IsStarted
                );
        }

        public bool IsInstalled { get; private set;} = false;
        public bool IsLaunched { get; private set; } = false;
        public PlayStateCombine PlayStateCombine { get; private set; }
        public event EventHandler<PlayStateCombine> PlayerStateChanged;
        public void Launch()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Launch, null));
                return;
            }
            if (!IsLaunched)
            {
                ServiceFactory.QQMusicService.OpenQQMusic("");
            }
        }
        public void Play(object item)
        {
            if(isSuspend)
            {
                commandList.Clear();
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Play,item));
                return;
            }
            if(item is string s)
            {
                ServiceFactory.QQMusicService.OpenQQMusic(s);
            }

        }
        public void Play()
        {
            if (isSuspend)
            {
                resumePauseState = false;
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.Play");
            ServiceFactory.QQMusicService.OpenQQMusic("/playcontrol play");
        }
        public void Stop()
        {
            if (isSuspend)
            {
                resumePauseState = true;
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.Stop");
            ServiceFactory.QQMusicService.OpenQQMusic("/playcontrol stop");
        }
        public void Pause()
        {
            if (isSuspend)
            {
                resumePauseState = true;
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.Pause");
            ServiceFactory.QQMusicService.OpenQQMusic("/playcontrol pause");
        }
        public void Prev()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Prev, null));
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.Prev");
            ServiceFactory.QQMusicService.OpenQQMusic("/playcontrol prev");
        }
        public void Next()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Next, null));
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.Next");
            ServiceFactory.QQMusicService.OpenQQMusic("/playcontrol next");
        }
        public void PlayByRandom()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByRandom, null));
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.PlayByRandom");
            ServiceFactory.QQMusicService.OpenQQMusic("/playmode 4");
        }
        public void PlayByOrder()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByOrder, null));
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.PlayByOrder");
            ServiceFactory.QQMusicService.OpenQQMusic("/playmode 2");
        }
        public void PlayByListLoop()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByListLoop, null)) ;
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.PlayByListLoop");
            ServiceFactory.QQMusicService.OpenQQMusic("/playmode 3");
        }
        public void PlayBySingleLoop()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayBySingleLoop, null));
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }
            Debug.WriteLine($"MusicPlayer.PlayBySingleLoop");
            ServiceFactory.QQMusicService.OpenQQMusic("/playmode 1");
        }
        public void FastForward(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.FastForward, seconds));
                return;
            }
            //qqmusic.exe /forward[rewind] 10
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }

            Debug.WriteLine($"MusicPlayer.FastForward");
            ServiceFactory.QQMusicService.OpenQQMusic("/forward " + seconds);
        }
        public void FastRewind(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.FastRewind, seconds));
                return;
            }
            //qqmusic.exe /forward[rewind] 10
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }

            Debug.WriteLine($"MusicPlayer.FastRewind");
            ServiceFactory.QQMusicService.OpenQQMusic("/rewind " + seconds);
        }
        public void SeekTo(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.SeekTo, seconds));
                return;
            }
            if (!IsInstalled || !IsLaunched)
            {
                return;
            }

            Debug.WriteLine($"MusicPlayer.SeekTo");
            ServiceFactory.QQMusicService.OpenQQMusic("/seekto " + seconds);
        }

        bool resumePauseState=false;
        bool isSuspend=true;
        public void Suspend()
        {
            if(!isSuspend)
            {
                commandList.Clear();
                resumePauseState = PlayStateCombine.PlayerState != PlayerState.Play;
                Debug.WriteLine($"MusicPlayer.Suspend");
                Pause();
                isSuspend = true;
            }
        }
        public void Resumed()
        {
            if (!isSuspend) return;
            isSuspend = false;
            bool flag = true;
            Debug.WriteLine($"MusicPlayer.Resumed");
            while (commandList.Count>0)
            {
                var command = commandList.Dequeue();
                switch (command.Key)
                {
                    case PlayerCommandType.Launch:
                        Launch();
                        break;
                    case PlayerCommandType.Play:
                        Play((string)command.Value);
                        break;
                    case PlayerCommandType.Prev:
                        Prev();
                        break;
                    case PlayerCommandType.Next:
                        Next();
                        break;
                    case PlayerCommandType.FastForward:
                        FastForward((int)command.Value);
                        break;
                    case PlayerCommandType.FastRewind:
                        FastRewind((int)command.Value);
                        break;
                    case PlayerCommandType.SeekTo:
                        SeekTo((int)command.Value);
                        break;
                    case PlayerCommandType.PlayByOrder:
                        PlayByOrder();
                        flag = false;
                        break;
                    case PlayerCommandType.PlayByRandom:
                        PlayByRandom();
                        flag = false;
                        break;
                    case PlayerCommandType.PlayByListLoop:
                        PlayByListLoop();
                        flag = false;
                        break;
                    case PlayerCommandType.PlayBySingleLoop:
                        PlayBySingleLoop();
                        flag = false;
                        break;
                    default:
                        break;
                }
                if (commandList.Count == 0 && flag) return;
            }
            if(!resumePauseState)
            {
                Play();
            }
        }

        public void Resumed2()
        {
            if (!isSuspend) return;
            isSuspend = false;
            commandList.Clear();
            Debug.WriteLine($"MusicPlayer.Resumed2");
            if (!resumePauseState)
            {
                Play();
            }
        }
    }
}
